
var app = getApp()
var fun = require('../fun.js');
Page({
  data: {
    nav:{top:app.globalData.nav_top,title:'个人中心',home:'show',back:'show',safe_top:app.globalData.safe_top,page:'my'},page:1,can_load:1,
  },

  onLoad: function (op) {
    fun.view_add('打开个人中心');
    var ac=op.ac;
    if(ac){this.data.ac=ac;}
    var id=op.id;
    if(id){this.data.id=id;}
    fun.view_add('打开个人中心');
  },

  onReady: function () {
    if(getCurrentPages().length==1){var nav=this.data.nav;nav.back='';this.setData({nav:nav})}
this.load();
var pages=getCurrentPages();
this.setData({page_num:Object.keys(pages).length})
  },

  onShow: function () {
if(app.globalData.flash==1){this.load()}
app.globalData.flash=''
  },


  load: function () {
    var shop=app.globalData.shop_id;
    var shop_id=0;
    if(shop){shop_id=shop;}
    var that=this;fun.view_add('加载中');
fun.get({ac:'my',shop:shop_id}).then(res=>{fun.view_add('加载完成');
  this.setData({list:res.list,
    user:res.user,
    icon:res.icon,
    set:res.set,
    info:res.info
  });
 // console.log(res.user.pro);
 if(res.user){
    /*
if(!res.user.phone){
wx.navigateTo({
  url: '/pages/my/edit',
})
}
*/

if(!res.user.pro && app.globalData.region){
   console.log(app.globalData.region)
   var region=app.globalData.region;
var data={pro:region[0],city:region[1],dist:region[2]}
fun.get({ac:'user_change',data:JSON.stringify(data)}).then(res=>{

})

}}else{
   app.globalData.user_id='';
   app.globalData.session='';
   fun.login().then(res=>{console.log(res)
      if(res.err=='ok'){
      this.setData({user:res.user});
      }
      if(res.is_new){
         this.setData({user_change_show:'show'})
      }
   })

}


var ac=this.data.ac;
var id=this.data.id;
if(ac=='emp_add' && id){
   if(res.user){
if(res.user.is_emp>0){app.err('抱歉，您已经绑定了门店，请取消绑定后重新绑定');}else{
wx.showModal({
  title: '提示',content:'你将提交绑定为门店的员工，是否继续？',success(res){
     if(res.confirm){
fun.get({ac:'emp_band',id:id}).then(res=>{
app.err('绑定成功，请等待管理员审核');
})
     }
  }
})

}

   }
}

})
  },

  onUnload: function () {

  },
  hexiao_code(e){//打开核销窗口并确认
    var index=e.currentTarget.dataset.index;
    var list=this.data.order;
    fun.get({ac:'get_order_qr',id:list[index].id}).then(res=>{
this.setData({hexiao_show:'show',hexiao_code:res.qr_img,hexiao_index:index})
    })
  },
  order_ok(e){
    var index=e.currentTarget.dataset.index;
    var list=this.data.order;
    var that=this;
    wx.showModal({
      title: '警告',content:'如果您没有收到商品，请勿进行确认收货，是否继续？',success(res){
  if(res.confirm){
    fun.get({ac:'order_ok',id:list[index].id}).then(res=>{
  list[index]['state']=10;
  that.setData({list:list,hexiao_show:''});
    })
  }
      }
    })
  
  },
  loginout: function () {//注销
    fun.view_add('注销用户');
var that=this;
wx.showModal({
  title: '提示',content:'是否退出登录?',success(res){
    if(res.confirm){
      try {
        wx.setStorageSync('login_info', '');
        wx.setStorageSync('adm_login', '');
      } catch (e) { }
app.globalData.adm_id='';
app.globalData.user_id='';
that.load()
    }
  }
})
  },
  hexiao(e){//核销订单
    fun.view_add('核销订单');
    var that=this;
wx.scanCode({
  success(res){
    console.log(res);

  //   
  wx.navigateTo({
    url: '/pages/order/detail?c=adm&code='+res.result,
  });
  return;
fun.get({ac:'hexiao',val:res.result}).then(res=>{
app.err('订单核销成功');
})
  }
})
  },
  scan_login(e){//扫描登录
   var that=this;
   wx.scanCode({
     success(res){
       console.log(res);
   fun.get({ac:'scan_login',val:res.result}).then(res=>{
  app.err('登录成功');
   })
     }
   })
  },
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },go(e){ wx.navigateTo({url: e.currentTarget.dataset.url})},
  back(e){
   var pages=getCurrentPages();
   if(Object.keys(pages).length==1){this.home();}else{wx.navigateBack({delta:0})}
   },
  home(e){ wx.reLaunch({url: '/pages/index/index'})},
  rgo(e){wx.redirectTo({url: e.currentTarget.dataset.url})},
  login: function (e) {wx.navigateTo({url: '/pages/login/index',})},
  win_open(e){var c=e.currentTarget.dataset.c;this.setData({[c]:'show'})},//打开窗口
  win_close(e){var c=e.currentTarget.dataset.c;this.setData({[c]:''})},//关闭窗口
open_min(e){wx.navigateToMiniProgram({appId: e.currentTarget.dataset.appid,path: e.currentTarget.dataset.url,})},
scan(e){fun.scan();},get_addr(){//获取微信联系人地址簿
   var that=this;
   wx.chooseAddress({
     success (res) {
   console.log(res)

     }
   })

 } , get_avatar(e){
   var user=this.data.user;if(!user){user={}}
   var user_edit_arr=this.data.user_edit_arr;
   if(!user_edit_arr){user_edit_arr={}}
   var url=e.detail.avatarUrl;
   console.log(e,url)
   fun.file_read(url).then(res=>{
      var data='data:image/jpeg;base64,'+res
      user.logo=data;
      user_edit_arr.logo=res;
      this.data.user_edit_arr=user_edit_arr;
   this.setData({user:user});
   this.submit_user_edit();
   });
},
user_nickname_change(e){
   var val=e.detail.value;
   console.log(val);
   var user_edit_arr=this.data.user_edit_arr;
   if(!user_edit_arr){user_edit_arr={}}
   user_edit_arr.nickname=val;
   user_edit_arr.is_change=1;
   this.setData({user_edit_arr:user_edit_arr})
},submit_user_edit(e){
   var user_edit_arr=this.data.user_edit_arr;if(!user_edit_arr){user_edit_arr={}}
   if(e){
   if(!user_edit_arr['nickname']){app.err('请输入您的昵称');return;}
   }
   fun.get({ac:'user_info_change',data:JSON.stringify(user_edit_arr)}).then(res=>{
this.setData({user_edit_arr:''});
if(e){this.setData({user_change_show:''})}
//
   })
}
})
